package com.feature50.makeover.ui;

import javax.swing.*;
import javax.swing.plaf.metal.MetalScrollBarUI;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;

public class CandylandScrollbarUI extends MetalScrollBarUI {
    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        c.setOpaque(false);
    }

    @Override
    protected JButton createDecreaseButton(int orientation) {
        decreaseButton = new CandylandScrollButton(orientation, scrollBarWidth, isFreeStanding);
        return decreaseButton;
    }

    @Override
    protected JButton createIncreaseButton(int orientation) {
        increaseButton = new CandylandScrollButton(orientation, scrollBarWidth, isFreeStanding);
        return increaseButton;
    }

    @Override
    public Dimension getPreferredSize(JComponent jComponent) {
        Dimension dimension = super.getPreferredSize(jComponent);
        dimension.width = 7;
        return dimension;
    }

    @Override
    protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
            int width = trackBounds.width;
            int height = trackBounds.height;

            g2.translate(trackBounds.x, trackBounds.y);

            RoundRectangle2D roundCasing = new RoundRectangle2D.Double(
                    0, 0, width, height, width, width);
            g2.setPaint(new LinearGradientPaint(0.0f, 0.0f, width, 0.0f,
                new float[] {
                    0.0f,
                    0.6f,
                    0.61f,
                    1.0f
                },
                new Color[] {
                    Color.BLACK,
                    Color.BLACK,
                    new Color(26, 26, 26),
                    Color.BLACK
                }));
            g2.fill(roundCasing);

            g2.translate(-trackBounds.x, -trackBounds.y);
        }
        g2.dispose();
    }

    @Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
            int width = thumbBounds.width;
            int height = thumbBounds.height;

            g2.translate(thumbBounds.x, thumbBounds.y);

            RoundRectangle2D casing = new RoundRectangle2D.Double(
                    0, 0, width, height, width, width);

            g2.setPaint(new LinearGradientPaint(0.0f, 0.0f, width, 0.0f,
                new float[] {
                    0.0f,
                    0.3f,
                    0.31f,
                    1.0f
                },
                new Color[] {
                    new Color(210, 210, 210),
                    new Color(210, 210, 210),
                    new Color(210, 210, 210),
                    new Color(184, 184, 184)
                }));
            g2.fill(casing);
//            g2.setPaint(paint);
//            g2.setColor(new Color(1.0f, 1.0f, 1.0f, 0.2f));
//            casing = new RoundRectangle2D.Double(
//                    0, 0, width - 1, height - 1, width, width);
//            g2.draw(casing);

            g2.setColor(Color.WHITE);
            g2.drawLine(2, 3, 2, 3 + (height - 6));
            g2.setColor(new Color(139, 139, 139));
            g2.drawLine(width - 1, 2, width - 1, 2 + (height - 5));

            g2.translate(-thumbBounds.x, -thumbBounds.y);
        }
        g2.dispose();
    }
}